const Joi = require("joi");

let BrandSchema = Joi.object({
    name: Joi.string().required().error(new Error('name参数填写错误')),
    logoName: Joi.string().required().error(new Error('logoName参数填写错误')),
    createBrandTime: Joi.string().required().error(new Error('createBrandTime参数填写错误')),
    createLocation: Joi.string().required().error(new Error('createLocation参数填写错误')),
    description: Joi.string().required().max(300).error(new Error('description参数填写错误')),
});

class BrandMiddleware {
    async brandValidator(ctx, next) {
        const bodyParams = ctx.request.body;
        const {id} = ctx.params;
        if (ctx.request.url.indexOf('update') > -1) {
            if (!id) {
                return ctx.body = {
                    status: 1,
                    msg: '未传菜单id'
                }
            }
        }
        if (bodyParams) {
            const {error} = BrandSchema.validate(bodyParams, {
                abortEarly: true
            });
            if (error) {
                return ctx.body = {
                    status: 1,
                    msg: error.message
                }
            }
        } else {
            return ctx.body = {
                status: 1,
                msg: '请填写必填参数'
            }
        }
        await next()
    }
}

module.exports = new BrandMiddleware();
